package com.tool.util;


import java.util.HashMap;
import java.util.Map;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.RowSet;

import com.kingdee.util.db.SQLUtils;
import com.tool.DBConnector;
import com.tool.DataCenterVO;
/**
 * 
 */
public class CachMap {
	
	public CachMap(DataCenterVO dbVo){
		this.dbVO=dbVo;
	}
	private DataCenterVO  dbVO ;
	
	/** 存货基本档案*/
	private  Map<String, String> MaterialMap = new HashMap<String, String>();
	/** 存货管理档案*/
	private  Map<String, String> invManageMap = new HashMap<String, String>();
	/** 单据类型 */
	private  Map<String, String> BillTypeMap = new HashMap<String, String>();

	/** 库存组织 */
	private  Map<String, String> StorageOrgUnitMap = new HashMap<String, String>();

	/** 公司 */
	private  Map<String, String> CompanyOrgUnitMap = new HashMap<String, String>();

	/** 采购组织*/
	private  Map<String, String> PurchaseOrgUnitMap = new HashMap<String, String>();

	/** 客商管理档案 */
	private  Map<String, String> CustSuppMap = new HashMap<String, String>();
	
	/** 客商基本档案 */
	private  Map<String, String> CustSuppBasIDMap = new HashMap<String, String>();

	/** 仓库 */
	private  Map<String, String> WarehouseMap = new HashMap<String, String>();

	/** 收发类别 */
	private  Map<String, String> DispatchMap = new HashMap<String, String>();
	
	/** 收发类别 */
	private  Map<String, String> DeptMap = new HashMap<String, String>();
	
	/** 人员 */
	private  Map<String, String> personMap = new HashMap<String, String>();
	
	/** 用户 */
	private  Map<String, String> userMap = new HashMap<String, String>();

	/**
	 * 通过编码获取存货基本档案ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getInvBasDoc(String number)  {
		if (!MaterialMap.containsKey(number)) {
			Connection conn = null;
			
			String materialID = null;

			StringBuilder headSql = new StringBuilder();

			// 返回存货ID
			headSql.append(" select pk_invbasdoc from bd_invbasdoc where invcode= '" + number + "'");
			
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, headSql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					materialID = rs.getString("pk_invbasdoc");
				}
				MaterialMap.put(number, materialID);
				//清除conn
				return materialID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return MaterialMap.get(number);
	}
	
	
	/**
	 * 通过编码获取存货管理档案ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getInvManageDoc(String baseId){
		if (!invManageMap.containsKey(baseId)) {
			Connection conn = null;
			
			String invManID = null;

			StringBuilder headSql = new StringBuilder();

			// 返回存货ID
			headSql.append("select * from bd_invmandoc  where pk_invbasdoc='" + baseId + "'");
			//select * from bd_invmandoc  where pk_invbasdoc='000102100000000018ZH'
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, headSql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					invManID = rs.getString("pk_invmandoc");
				}
				invManageMap.put(baseId, invManID);
				return invManID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return invManageMap.get(baseId);
	}
	
	
	/**
	 * 通过编码获取库存组织ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getStorageOrgUnit(String number) {
		if (!StorageOrgUnitMap.containsKey(number)) {
			Connection conn = null;
			String stoOrgID = null;
			StringBuilder sql = new StringBuilder();
			// 返回收发类别ID
			sql.append(" select * from bd_calbody where bodycode= '" + number + "'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					stoOrgID = rs.getString("pk_calbody");
				}
				StorageOrgUnitMap.put(number, stoOrgID);
				//清除conn
				return stoOrgID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return StorageOrgUnitMap.get(number);
	}
	
	
	/**
	 * 方法需改进:每次初始化时自动把所有收发类别装载进Map
	 * 通过编码获取收发类别ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getDispatch(String number) {
		if (!DispatchMap.containsKey(number)) {
			Connection conn = null;
			String DispatchID = null;
			StringBuilder headSql = new StringBuilder();
			// 返回收发类别ID
			headSql.append(" select * from bd_rdcl where rdcode= '" + number + "'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, headSql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					DispatchID = rs.getString("pk_rdcl");
				}
				DispatchMap.put(number, DispatchID);
				return DispatchID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return DispatchMap.get(number);
	}

	/**
	 * 通过编码获取仓库ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getDept(String number) {
		if (!DeptMap.containsKey(number)) {
			Connection conn = null;
			String DeptID = null;
			StringBuilder sql = new StringBuilder();
			// 返回收发类别ID
			sql.append(" select * from bd_deptdoc where deptcode= '" + number + "'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					DeptID = rs.getString("pk_deptdoc");
				}
				DeptMap.put(number, DeptID);
				//清除conn
				return DeptID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return DeptMap.get(number);
	}
	
	/**
	 * 通过编码获取仓库ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getWarehouse(String number) {
		if (!WarehouseMap.containsKey(number)) {
			Connection conn = null;
			String WarehouseID = null;
			StringBuilder headSql = new StringBuilder();
			// 返回收发类别ID
			headSql.append(" select * from bd_stordoc where storcode= '" + number + "'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, headSql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					WarehouseID = rs.getString("pk_stordoc");
				}
				WarehouseMap.put(number, WarehouseID);
				//清除conn
				return WarehouseID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return WarehouseMap.get(number);
	}
	

	/**
	 * 通过编码获取客商ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getCustSupp(String number,String orgId) {
		if (!CustSuppMap.containsKey(number)) {
			Connection conn = null;
			String custSuppID = null;
			StringBuilder sql = new StringBuilder();
			// 返回客商ID
			sql.append("select * from bd_cubasdoc t1  left outer join bd_cumandoc t2 " +
					   "on t1.pk_cubasdoc=t2.pk_cubasdoc where t1.custcode ='"+number+"' and  t2.pk_corp='"+orgId+"'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					custSuppID = rs.getString("pk_cumandoc");
				}
				CustSuppMap.put(number, custSuppID);
				return custSuppID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return CustSuppMap.get(number);
	}
	

	/**
	 * 通过编码获取客商基本档案ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getCustSuppBasID(String number) {
		if (!CustSuppBasIDMap.containsKey(number)) {
			Connection conn = null;
			String custSuppID = null;
			StringBuilder sql = new StringBuilder();
			// 返回客商ID
			sql.append("select * from bd_cubasdoc where custcode ='"+number+"'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					custSuppID = rs.getString("pk_cubasdoc");
				}
				CustSuppBasIDMap.put(number, custSuppID);
				return custSuppID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return CustSuppBasIDMap.get(number);
	}
	
	
	/**
	 * 通过编码获取公司ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getCorp(String number) {
		if (!CompanyOrgUnitMap.containsKey(number)) {
			Connection conn = null;
			String custSuppID = null;
			StringBuilder sql = new StringBuilder();
			// 返回客商ID
			sql.append("select * from bd_corp where unitcode='"+number+"'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					custSuppID = rs.getString("pk_corp");
				}
				CompanyOrgUnitMap.put(number, custSuppID);
				return custSuppID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return CompanyOrgUnitMap.get(number);
	}
	
	/**
	 * 通过编码获取员工管理档案ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getPerson(String number) {
		if (!personMap.containsKey(number)) {
			Connection conn = null;
			String personID = null;
			StringBuilder sql = new StringBuilder();
			// 返回客商ID
			sql.append("select * from bd_psndoc where psncode='"+number+"'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					personID = rs.getString("pk_psndoc");
				}
				personMap.put(number, personID);
				return personID;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return personMap.get(number);
	}
	/**
	 * 通过编码获取用户ID
	 * @param number
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public  String getUser(String number) {
		if (!userMap.containsKey(number)) {
			Connection conn = null;
			String userId = null;
			StringBuilder sql = new StringBuilder();
			// 返回客商ID
			sql.append("select * from bd_psndoc p left outer join sm_userandclerk u on  p.pk_psnbasdoc=u.pk_psndoc  where p.psncode='"+number+"'");
			try {
				conn = DBConnector.getJDBCConnection(dbVO);
				RowSet rs=SQLUtils.getJdbcRowSet(conn, sql.toString());
				while (rs.next()) {
					// 保存表头之后，将记录ID返回，设置分录的父ID
					userId = rs.getString("userid");
				}
				userMap.put(number, userId);
				return userId;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				SQLUtils.cleanup(conn);
			}
		}
		return userMap.get(number);
	}







}
